<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class spiderAction extends BackAction {
    
    private $users      = array();
    private $weibo_id   = null;

    protected function set_users($users){
        foreach ($users as $k => $v) {
            $this->users[]  = $v;
        }        
    }
    
    /**
     * 地域分析
     */
    private function _location(){
        
        if($users !=null){
            $this->users    = $users;
        }
        
        $loc    = array();
        
        foreach ($this->users as $k1 => $v1) {
            if(empty($v1['location'])){
                $loc['未知'][]  = '未知';
                continue;
            }
            $location   = explode(' ', $v1['location']);
            $loc[$location[0]][]    = $v1['location'];
        }
        
        foreach ($loc as $k3 => $v3) {
            DB_LOCATION::saveLocation($this->weibo_id, $k3, count($v3));
        }
    }

    /**
     * 性别分析
     */
    private function _sex(){
        
        foreach ($this->users as $k1 => $v1) {
            $g  = $v1['gender'];
            $gender[$g][]  = $g;
        }
        
        foreach ($gender as $k2 => $v2) {
            DB_SEX::saveSex($this->weibo_id, $k2, count($v2));
        }
    }
    
    function _getData($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $fp = curl_exec($ch);
        curl_close($ch);
        return $fp;
    }
    
    /**
     * 抓取标签
     */
    function actionTag() {
        $weibo_id   = $this->_get_param_json_weibo_id();
        $url        = "http://".$_SERVER['HTTP_HOST'].'/json/tag?weibo_id='.$weibo_id;
        $fp         = $this->_getData($url);
        //echo $fp;
        $re = null;
        if(trim($fp)!='error'){
            $re = DB_TAG::store($weibo_id, $fp);
        }
        $this->_json_ok($fp);
    }
    
    /**
     * 获取我的粉丝
     */
    function actionApiFollow(){
        $weibo_id       = $this->_get_param_json_weibo_id();
        $this->weibo_id = $weibo_id;
        $token          = $this->_get_param_json_token();
        $analysis_user  = ORM_Finder::findOne('DB_ANALYSIS_USER', array('weibo_id'=>$weibo_id));
        
        //已分析过
        if($analysis_user){
            $this->_json_ok('ok');
        }
        
        $c  = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);
            
        //待分析的微博信息
        $followers  = $this->_exception($c->show_user_by_id($weibo_id));
        
        //获取粉丝列表
        $re = $this->_exception($c->followers_by_id($weibo_id, 0, 200));

        $this->set_users($re['users']);
       //存储到粉丝表
        DB_FOLLOWS::batchSave($weibo_id, $re['users']);

        while($re['next_cursor']){
            //echo $re['next_cursor'].'<br/>';
            $re = $this->_exception($c->followers_by_id($weibo_id, $re['next_cursor'], 200));
            
            //存储到粉丝表
            DB_FOLLOWS::batchSave($weibo_id, $re['users']);

            $this->set_users($re['users']);
        }
        
        $followers['fetch_count']   = count($this->users);

        //保存分析的用户信息
        $u  = DB_ANALYSIS_USER::batch_save(array($followers)); 
        //分析地域
        $this->_location();
        //分析性别
        $this->_sex();
        
        $this->_json_ok('ok');
    }
    
    
    /**
     * 抓取blog
     */
    function actionApiBlog(){

        $weibo_id   = $this->_get_param_json_weibo_id();
        $token      = $this->_get_param_json_token();
     
        $c          = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);
        $re         = $this->_exception($c->user_timeline_by_id($weibo_id));
        $result     = array();
        $json       = array();
        
        //将转发的添加到数组
        foreach ($re['statuses'] as $k2 => $v2) {
            $result[]   = $v2;
            if(isset($v2['retweeted_status'])){
                $result[]   = $v2['retweeted_status'];
            }            
        }

        //存储信息
        foreach ($result as $k1 => $v1) {
            $blog   = DB_BLOG::store($weibo_id, $v1);
            $json[] = $blog->toArray();
        }
        $this->_json_ok($json);
    }
    
    /**
     * 最后发布微博时间
     */
    function actionLastPublish(){
        $token      = $this->_get_param_json_token();
        if(isset ($this->params['limit'])){
            $limit  = $this->params['limit'];
        }else{
            $limit  = 0;
        }
        
        $c          = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);
        $rs         = ORM_Finder::find('DB_WEIBO', array('last_publish'=>'0000-00-00 00:00:00'), '*', 'id asc', "0,1600");
        
        if(!$rs){
            echo 'no';
            exit;
        }
        
        foreach ($rs as $k => $v) {
            $weibo_id   = $v->getWeiboId();
            $re         = $this->_exception($c->user_timeline_by_id($weibo_id, 1,1));
            if(!isset($re['statuses'][0])){
                $up     = date('Y-m-d',strtotime('1970-01-01 00:00:00'));
            }else{
                $up         = date('Y-m-d H:i:s' ,strtotime($re['statuses'][0]['created_at']));
            }
            $v->setLastPublish($up);
            $v->save();
            echo $weibo_id.'<br/>';
        }
        echo "ok";
    }
    
    /**
     * 获取平均活跃度
     */
    function actionDayPublish(){
        $rs = ORM_Finder::find('DB_WEIBO', array('day_publish'=>'n'),'*', 'id asc', '5000');
        if(!$rs){
            echo 'no data';
            exit;
        }
        
        foreach ($rs as $k => $v) {
            echo $v->getWeiboId().'<br/>';
            $statuses_count = $v->getStatusesCount();
            if(!$statuses_count){
                $day_publish    = 0;
            }else{
                $time   = time() - strtotime($v->getCreatedAt());
                $time   = round($time /(3600*24),0);
                $day_publish = round($statuses_count / $time, 4);
            }
            $v->setDayPublish($day_publish);
            $v->save();
        }
    } 

    
    /**
     * 抓取评了
     * @param $weibo_id
     * @param $blog_id
     * @param $token
     */
    function actionApiComment(){
        $weibo_id       = $this->_get_param_json_weibo_id();
        $token          = $this->_get_param_json_token();
        $blog_id        = $this->params['blog_id'];
        
        $c          = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);
        $comments   = ORM_Finder::find('DB_BLOG', "comment_up='0000-00-00 00:00:00' and comments_count>0", 'id,idstr', 'id asc', 100);
        $store      = array();
        
        /**
         * 未指定博客，没数据不进行更新
         */
        if(!$comments && !$blog_id){
            $this->_json_error('no comments to update.');
        }
        
        foreach ($comments as $k1 => $v1) {
            $re = $this->_ex($c->get_comments_by_sid($v1->getIdstr(), 1, 200));
            //更新或保存评论
            $save = DB_COMMENT::store($re['comments']);
            $store = array_merge($store, $save);
            $v1->setCommentUp(date('Y-m-d H:i:s'));
            $v1->save();
        }
        
        $this->_json_ok($store);
    }
    
    function actionApiCommentByMe(){
        $token          = $this->_get_param_json_token();
        $c              = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);
        $comments       = $this->_exception($c->comments_by_me(1, 200));
        print_r($comments);
        exit;
        $blogs  = array();
        $comments_new   = array();
        foreach ($comments['comments'] as $k1 => $v1) {
            //转发
            if(isset($v1['status']['retweeted_status'])){
                $blogs[]    = $v1['status']['retweeted_status'];
                //unset($v1['status']['retweeted_status']);
                $blogs[]    = $v1['status'];
            }
            if(isset($v1['reply_comment'])){
                $v1['reply_comment']['status']  = $v1['status'];
                $comments_new[] = $v1['reply_comment'];
            }
            
            unset($v1['reply_comment']);
            $comments_new[] = $v1;
        }
        
        //获取评论抓取配置
        $oauth          = ORM_Finder::findOne(DB_OAUTH, array('token'=>$token));
        if(!$oauth){
            $this->_json_error('no this token');
        }
        $weibo_id       = $oauth->getWeiboId();
        $config         = DB_ANALYSIS_CONFIG::store($weibo_id, 'comment_by_me', 'y');
        $is_new         = $config->isNew();
        //第一次抓取
        while($comments['next_cursor'] && $is_new){
            $comments       = $this->_exception($c->comments_by_me(1, 200));
        }
    }
    
    function actionSina(){
        $url    ="http://data.weibo.com/mydata/flash/myinfluencev?type=influence&_t=0&__rnd=".time();
        $ch     = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_COOKIE, 'UOR=weibo.com,weibo.com,; ULV=1321236347740:2:2:1:6402541034763.206.1321236347736:1320747231843; un=mknet201@sina.com; _s_tentry=login.sina.com.cn; Apache=6402541034763.206.1321236347736; SINAGLOBAL=6402541034763.206.1321236347736; SUE=es%3D83e9c3f223e97e518dcf1aa6cd8fd6d5%26ev%3Dv1%26es2%3D2474fc03fa596043faa37ea2e192db98%26rs0%3Dv1I5lU0of9tRkig6eu%252BRnJCpQPKiUkfMfzs91IKJjUCN%252F0U6vSWCnlOBxPdvGlL0OldcugMilRtDPfxfWcV%252BIL%252FFZRS7orSxMuUxo9gTx7H6%252FbdpkzUdpcUDQd34ijW%252BV5f8ljNDuBjmBy6a%252FRdVWDg8B1BaryoRbrqP%252F0yj1eE%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1321236411%26et%3D1321322811%26lt%3D1%26uid%3D2350462347%26user%3Dmknet201.%252A%252A%26ag%3D9%26name%3Dmknet201%2540sina.com%26nick%3Dmknet201%26sex%3D%26ps%3D0%26email%3Dmknet201%2540sina.com%26dob%3D%26ln%3D2350462347%26os%3D%26fmp%3D%26lcp%3D%26us%3D0%26vf%3D0; ALF=1321841212; SSOLoginState=1321236412; wvr=3.6; WNP=2350462347%2C47; NSC_wjq_wpuf.u.tjob.dpn.do=ffffffff0941113645525d5f4f58455e445a4a423660; __utma=182865017.1306690665.1321251291.1321251291.1321251291.1; __utmb=182865017.2.10.1321251291; __utmc=182865017; __utmz=182865017.1321251291.1.1.utmcsr=data.weibo.com|utmccn=(referral)|utmcmd=referral|utmcct=/mydata');
        curl_setopt($ch, CURLOPT_REFERER, 'http://data.weibo.com/mydata');
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
        $fp = curl_exec($ch);
        //$error  = curl_errno($ch);
        curl_close($ch);
        $fp = json_decode($fp, true);
        foreach ($fp['data']['list'] as $k => $v) {
//            $arr    = explode(':', $v);
//            echo $arr[0].'--'.$arr[1].'<br/>';
            echo $v.'<br/>';
        }
    }
    
    function actionHomeTimeline(){
        $str    = '1232342e342343';
        if(preg_match('~^\d*$~', $str)){
            echo 111;
        }else{
            echo 222;
        }
        
        $index  = 0;
        foreach ($array2 as $k2 => $v2) {
            if($v2['dname'] != $array1[$index][2]){
                //插入到数据库吧
            }
        }
    }
    
    function actionShua(){
        $url    ="http://bbs.onlylady.com/thread-htm-fid-7.html";
        $ch     = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_REFERER, 'http://http://bbs.onlylady.com');
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
        $fp = curl_exec($ch);
        //$error  = curl_errno($ch);
        curl_close($ch);
        print_r($fp);
    }

}

?>
